import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Main {
    static String defaultDir = "/Users/liuzy/Downloads/";
    static String defaultExcel = "/Users/liuzy/Downloads/jwc.xls";

    public static void main(String[] args) {
        String dirPath = (args != null && args.length > 0 && args[0] != null) ? args[0] : defaultDir;
        File dir = new File(dirPath);
        if (!dir.exists() || !dir.isDirectory()) {
            System.out.println("文件夹不存在或不是一个文件夹！");
            return;
        }
        List<File> pdfs = new ArrayList<>();
        allPdfs(dir, pdfs);
        for (File pdf : pdfs) {
            System.out.println("==>[" + pdf.getPath() + "]");
            String[] lines = pdfText(pdf);
            for (String line : lines) {
                System.out.println(line);
            }
        }
        saveExcel(); // 测试写excel
    }

    public static void saveExcel() {
        List<MainData> datas = new ArrayList<>();
        datas.add(new MainData("测试", new Date(), 2.01));
        datas.add(new MainData("测试2", new Date(), 1000.01));

        try {
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet("sheet1");
            for (int i = 0; i < datas.size(); i++) {
                MainData d = datas.get(i);
                HSSFRow row = sheet.createRow(i);
                HSSFCell cell = row.createCell(0);
                cell.setCellValue(d.getString());
                cell = row.createCell(1);
                cell.setCellValue(d.getDate());
                cell = row.createCell(2);
                cell.setCellValue(d.getNum());
            }
            FileOutputStream os = new FileOutputStream(defaultExcel);
            wb.write(os);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void allPdfs(File dir, List<File> pdfs) {
        if (!dir.exists() || !dir.isDirectory()) {
            return;
        }
        String[] files = dir.list();
        if (files != null) {
            for (String path : files) {
                File file = new File(dir, path);
                if (file.isDirectory()) {
                    allPdfs(file, pdfs);
                } else {
                    if (path.toLowerCase().endsWith(".pdf")) {
                        pdfs.add(file);
                    }
                }
            }
        }
    }

    public static String[] pdfText(File file) {
        try (FileInputStream fis = new FileInputStream(file)) {
            PdfReader pdfReader = new PdfReader(fis);
            int totalPage = pdfReader.getNumberOfPages();
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i <= totalPage; i++) {
                sb.append(PdfTextExtractor.getTextFromPage(pdfReader, i)).append("\n");
            }
            pdfReader.close();
            return sb.toString().trim().split("\n");
        } catch (Exception e) {
            e.printStackTrace();
            return new String[0];
        }
    }
}
